/**
 * 
 */
package org.meruvian.yama.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.meruvian.yama.entity.Person;
import org.meruvian.yama.entity.User;
import org.meruvian.yama.form.UserForm;
import org.meruvian.yama.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
*
* @author vick
*/
@Controller
public class UserController {

	@Autowired
	private UserService userService;

	
	@RequestMapping(value="/user/login", method=RequestMethod.GET)
	public Map<String, Object> login(HttpServletResponse response){

		Map<String, Object> model = new HashMap<String, Object>();
		model.put("encoding", response.getCharacterEncoding());
		model.put("content-type", response.getContentType());
		model.put("status", "success");
		model.put("user", new User());
		
		return model;
	}
	
	@RequestMapping(value="/user/login", method=RequestMethod.POST)
	public ModelAndView login(HttpServletResponse response, @ModelAttribute("user") User u){

	    User user = userService.getByUserPassword(u.getUsername(),
	    		DigestUtils.md5Hex(u.getPassword().getBytes()));

		if (user == null) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("encoding", response.getCharacterEncoding());
			model.put("content-type", response.getContentType());
			model.put("status", "failed");
			ModelAndView mv = new ModelAndView("user/model");
			mv.addObject("model",model.toString());
			
			return mv;
		}

		user.setSecret(UUID.randomUUID().toString());
		userService.save(user);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("encoding", response.getCharacterEncoding());
		model.put("content-type", response.getContentType());
		model.put("status", "success");
		model.put("secret", user.getSecret());
		model.put("id", user.getPerson().getId());
		
		ModelAndView mv = new ModelAndView("user/model");
		mv.addAllObjects(model);
		
		return mv;
	}
	
	@RequestMapping(value="/user/register", method=RequestMethod.POST)
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response,@RequestParam("password") String password,@RequestParam("gender") String gender, @ModelAttribute("user") UserForm user,@RequestParam("bbirthdate") String birthDate){
        	Person person = user.getPerson(); 
        	person.setGender(Boolean.parseBoolean(gender));
			user.setPassword(DigestUtils.md5Hex(user.getPassword().getBytes()));

			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			Date birthdate = new Date();

			try {
				birthdate = format.parse(birthDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			person.setBirthDate(birthdate);

			user.setPerson(person);
			userService.save(user.getUser());
			response.setContentType("text/html");
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("encoding", response.getCharacterEncoding());
			model.put("content-type", response.getContentType());
			model.put("status", "success");
			model.put("username", user.getUsername());
			ModelAndView mv = new ModelAndView("user/model");
			mv.addObject("model",model.toString());
			
		return mv;
	}
	
	
//	@Override
//	public Object getModel() {
//		return model.size() > 0 ? model : user;
//	}
	
	
//	private Map<String,Object> getModelMapError(String msg){
//
//		Map<String,Object> modelMap = new HashMap<String,Object>(2);
//		modelMap.put("message", msg);
//		modelMap.put("success", false);
//
//		return modelMap;
//	} 
}
